/**
 * @license
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Class constructor for Radio MDL component.
 * Implements MDL component design pattern defined at:
 * https://github.com/jasonmayes/mdl-component-design-pattern
 * @param {HTMLElement} element The element that will be upgraded.
 */
class MaterialRadio {

  constructor (element) {

    this.element_ = element;

    // Initialize instance.
    this.init();
  }

  /**
   * Store constants in one place so they can be updated easily.
   * @enum {string | number}
   * @private
   */
  get Constant_ () {
    return {
      TINY_TIMEOUT: 0.001
    };
  }

  /**
   * Store strings for class names defined by this component that are used in
   * JavaScript. This allows us to simply change it in one place should we
   * decide to modify at a later date.
   * @enum {string}
   * @private
   */
  get CssClasses_ () {
    return {
      IS_FOCUSED: 'is-focused',
      IS_DISABLED: 'is-disabled',
      IS_CHECKED: 'is-checked',
      IS_UPGRADED: 'is-upgraded',
      JS_RADIO: 'mdl-js-radio',
      RADIO_BTN: 'mdl-radio__button',
      RADIO_OUTER_CIRCLE: 'mdl-radio__outer-circle',
      RADIO_INNER_CIRCLE: 'mdl-radio__inner-circle',
      RIPPLE_EFFECT: 'mdl-js-ripple-effect',
      RIPPLE_IGNORE_EVENTS: 'mdl-js-ripple-effect--ignore-events',
      RIPPLE_CONTAINER: 'mdl-radio__ripple-container',
      RIPPLE_CENTER: 'mdl-ripple--center',
      RIPPLE: 'mdl-ripple'
    };
  }

  /**
   * Handle change of state.
   * @param {Event} event The event that fired.
   * @private
   */
  onChange_ (event) {

    this.updateClasses_(this.btnElement_, this.element_);

    // Since other radio buttons don't get change events, we need to look for
    // them to update their classes.
    var radios = document.getElementsByClassName(this.CssClasses_.JS_RADIO);
    for (var i = 0; i < radios.length; i++) {
      var button = radios[i].querySelector('.' + this.CssClasses_.RADIO_BTN);
      // Different name == different group, so no point updating those.
      if (button.getAttribute('name') === this.btnElement_.getAttribute('name')) {
        this.updateClasses_(button, radios[i]);
      }
    }
  }

  /**
   * Handle focus.
   * @param {Event} event The event that fired.
   * @private
   */
  onFocus_ (event) {

    this.element_.classList.add(this.CssClasses_.IS_FOCUSED);
  }

  /**
   * Handle lost focus.
   * @param {Event} event The event that fired.
   * @private
   */
  onBlur_ (event) {

    this.element_.classList.remove(this.CssClasses_.IS_FOCUSED);
  }

  /**
   * Handle mouseup.
   * @param {Event} event The event that fired.
   * @private
   */
  onMouseup_ (event) {

    this.blur_();
  }

  /**
   * Update classes.
   * @param {HTMLElement} button The button whose classes we should update.
   * @param {HTMLElement} label The label whose classes we should update.
   * @private
   */
  updateClasses_ (button, label) {

    if (button.disabled) {
      label.classList.add(this.CssClasses_.IS_DISABLED);
    } else {
      label.classList.remove(this.CssClasses_.IS_DISABLED);
    }

    if (button.checked) {
      label.classList.add(this.CssClasses_.IS_CHECKED);
    } else {
      label.classList.remove(this.CssClasses_.IS_CHECKED);
    }
  }

  /**
   * Add blur.
   * @private
   */
  blur_ (event) {

    // TODO: figure out why there's a focus event being fired after our blur,
    // so that we can avoid this hack.
    window.setTimeout(function() {
      this.btnElement_.blur();
    }.bind(this), this.Constant_.TINY_TIMEOUT);
  }

  // Public methods.

  /**
   * Disable radio.
   * @public
   */
  disable () {

    this.btnElement_.disabled = true;
    this.updateClasses_(this.btnElement_, this.element_);
  }

  /**
   * Enable radio.
   * @public
   */
  enable () {

    this.btnElement_.disabled = false;
    this.updateClasses_(this.btnElement_, this.element_);
  }

  /**
   * Check radio.
   * @public
   */
  check () {

    this.btnElement_.checked = true;
    this.updateClasses_(this.btnElement_, this.element_);
  }

  /**
   * Uncheck radio.
   * @public
   */
  uncheck () {

    this.btnElement_.checked = false;
    this.updateClasses_(this.btnElement_, this.element_);
  }

  /**
   * Initialize element.
   */
  init () {

    if (this.element_) {
      this.btnElement_ = this.element_.querySelector('.' +
          this.CssClasses_.RADIO_BTN);

      var outerCircle = document.createElement('span');
      outerCircle.classList.add(this.CssClasses_.RADIO_OUTER_CIRCLE);

      var innerCircle = document.createElement('span');
      innerCircle.classList.add(this.CssClasses_.RADIO_INNER_CIRCLE);

      this.element_.appendChild(outerCircle);
      this.element_.appendChild(innerCircle);

      var rippleContainer;
      if (this.element_.classList.contains(
          this.CssClasses_.RIPPLE_EFFECT)) {
        this.element_.classList.add(
            this.CssClasses_.RIPPLE_IGNORE_EVENTS);
        rippleContainer = document.createElement('span');
        rippleContainer.classList.add(
            this.CssClasses_.RIPPLE_CONTAINER);
        rippleContainer.classList.add(this.CssClasses_.RIPPLE_EFFECT);
        rippleContainer.classList.add(this.CssClasses_.RIPPLE_CENTER);
        rippleContainer.addEventListener('mouseup', this.onMouseup_.bind(this));

        var ripple = document.createElement('span');
        ripple.classList.add(this.CssClasses_.RIPPLE);

        rippleContainer.appendChild(ripple);
        this.element_.appendChild(rippleContainer);
      }

      this.btnElement_.addEventListener('change', this.onChange_.bind(this));
      this.btnElement_.addEventListener('focus', this.onFocus_.bind(this));
      this.btnElement_.addEventListener('blur', this.onBlur_.bind(this));
      this.element_.addEventListener('mouseup', this.onMouseup_.bind(this));

      this.updateClasses_(this.btnElement_, this.element_);
      this.element_.classList.add(this.CssClasses_.IS_UPGRADED);
    }
  }
}

var radios = document.querySelectorAll('.mdl-js-radio');
for (var r = 0; r < radios.length; r++) {
  new MaterialRadio(radios[r]);
}
